package top.suven.base.http.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.concurrent.ScheduledExecutorFactoryBean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import top.suven.base.http.Interceptor.*;
import top.suven.base.http.handler.JsonReturnHandlerArgumentResolver;
import top.suven.base.http.handler.ReturnHandlerArgumentResolver;

import java.util.List;

/**
 * 所有应用
 */
@Configuration
@EnableWebMvc
public class InterceptorConfiguration extends WebMvcConfigurerAdapter {//WebMvcConfigurerAdapter WebSecurityConfigurerAdapter


    @Autowired
    ApplicationContext applicationContext;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        // 注册拦截器
        final String PATH_PATTERNS = "/**";
        final String JVM_PATH_PATTERNS = "/jvm/**";//exclude
        super.addInterceptors(registry);
        // 配置不拦截的路径
//        interceptor.excludePathPatterns("/**.html");
        registry.addInterceptor(new LogbackTraceIdHandlerInterceptor()).addPathPatterns(PATH_PATTERNS);

        registry.addInterceptor(new JsonHandlerInterceptorAdapter()).addPathPatterns(PATH_PATTERNS);
        // 还可以在这里注册其它的拦截器
        //url下架直接返回
        registry.addInterceptor(new UrlHandlerInterceptor()).addPathPatterns(PATH_PATTERNS);
        //所有post请求，版本提示强制升级返回
        registry.addInterceptor(new VersionHandlerInterceptor()).addPathPatterns(PATH_PATTERNS);
        //所有post请求，用户在黑名单或设备在黑名单中，直接返回
        //registry.addInterceptor(new WhiteHandlerInterceptor()).addPathPatterns("/**");
        registry.addInterceptor(new BlackHandlerInterceptor()).addPathPatterns(PATH_PATTERNS);
        //参数校验，公共参数检查
        registry.addInterceptor(new ParamerHandlerInterceptor()).addPathPatterns(PATH_PATTERNS);
        //结合白名单和登录验证
        registry.addInterceptor(new TokenHandlerInterceptor(applicationContext)).addPathPatterns(PATH_PATTERNS);
    }
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        super.addArgumentResolvers(argumentResolvers);
        //与spring的参数验证有冲突，相当于所有参数自己解析处理
        //argumentResolvers.add(new JsonHandlerArgumentResolver());
        argumentResolvers.add(new JsonReturnHandlerArgumentResolver());
    }


    @Override
    public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
        super.addReturnValueHandlers(returnValueHandlers);
        returnValueHandlers.add(new ReturnHandlerArgumentResolver());
    }

//    @Bean
//    public AsyncConfig asyncConfig(){
//        return new AsyncConfig();
//    }
//
//    @Bean("taskExecutor")
//    public AsyncTaskExecutor asyncTaskExecutor(AsyncConfig asyncConfig) {
//        if (asyncConfig.getAsyncCorePoolSize() <= 0) {
//            asyncConfig.setAsyncCorePoolSize(Runtime.getRuntime().availableProcessors() * 2);
//        }
//        if (asyncConfig.getAsyncMaxPoolSize() <= 0) {
//            asyncConfig.setAsyncCorePoolSize(Math.max(Runtime.getRuntime().availableProcessors() * 2, asyncConfig.getAsyncCorePoolSize()));
//        }
//
//        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
//        taskExecutor.setCorePoolSize(asyncConfig.getAsyncCorePoolSize());
//        taskExecutor.setMaxPoolSize(asyncConfig.getAsyncMaxPoolSize());
//        taskExecutor.setKeepAliveSeconds(asyncConfig.getAsyncKeepAliveSeconds());
//        taskExecutor.setQueueCapacity(asyncConfig.getAsyncQueueCapacity());
//        taskExecutor.setAllowCoreThreadTimeOut(asyncConfig.isAsyncAllowCoreThreadTimeOut());
//        taskExecutor.initialize();
//
//        return taskExecutor;
//    }
//
//    @Bean("scheduledExecutor")
//    public ScheduledExecutorFactoryBean scheduledExecutor(AsyncConfig asyncConfig) {
//        ScheduledExecutorFactoryBean factory = new ScheduledExecutorFactoryBean();
//        factory.setPoolSize(asyncConfig.getPoolSize());
//        return factory;
//    }
//
//    @Bean
//    public AsyncService asyncService(AsyncConfig asyncConfig) {
//        return new AsyncService(scheduledExecutor(asyncConfig).getObject());
//    }
}
